package com.sgb.kjwl.viewmodel.self.packers;

import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.net.entity.ListResponse;
import com.swgk.core.base.api.ICallBack;
import com.sgb.kjwl.model.entity.FindPackersEntity;
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity;
import com.sgb.kjwl.view.ui.activity.self.packers.SelfPackersManageActivity2;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MLog;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class SelfPackersManageModel2 extends BaseViewModel {

    private SelfPackersManageActivity2 activity;

    private int page = 1;
    private int limit = 10;

    public SelfPackersManageModel2(SelfPackersManageActivity2 activity) {
        this.activity = activity;
    }

    public void pageTeamInfo(boolean isLoadMore) {
        page = isLoadMore ? page + 1 : 1;
        Map<String, Object> map = new HashMap<>();
        map.put("page", page);
        map.put("limit", limit);
        String companyNo = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.COMPANY_NO, "");
        int userType = SharedPreferenceUtil.getInstance().getSaveIntData(SharedPreferenceUtil.COM_TYPE, 0);
        if ("".equals(companyNo) || "0".equals(companyNo)) {
            map.put("userNo", SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.USER_NO, ""));
        } else if (userType == 2){
            map.put("teamNo", SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.COMPANY_NO, ""));
        }else {
            map.put("companyNo", companyNo);
        }
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        NewAPIManager.getInstance().getTeamNewInterfaceAPI().pageTeamInfoList(map).enqueue(new Callback<BaseEntity<ListResponse<List<FindPackersEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<FindPackersEntity>>>> call, Response<BaseEntity<ListResponse<List<FindPackersEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    activity.showList(response.body().getData(), isLoadMore, response.body().getData() == null || response.body().getData().getList().size() < limit);
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<FindPackersEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MLog.d(AppConstant.API_LOG, "error " + t.getMessage());
                if (!isLoadMore) {
                    activity.showEmpty();
                }
            }
        });
    }





    /**
     * 删除工队信息
     *
     * @param entity
     */
    public void deleteTeam(FindPackersEntity entity) {
        Map<String,Object> map = new HashMap<>();
        map.put("no", entity.getNo());
        NewAPIManager.getInstance().getTeamNewInterfaceAPI().delTeamList(map).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(AppConstant.REQEUST_SECCUESS)) {
                    MToast.showToast(activity, "删除成功");
                    pageTeamInfo(false);
                } else {
                    assert response.body() != null;
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {

            }
        });
    }

    /**
     * 跳转到h5详情
     *
     * @param no
     */
    public void TeamParticulars(String no) {
        DialogHelper.showProgressDialog(activity, null, "加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().getUrlSetting("teamWorkDetail").enqueue(new ICallBack<BaseEntity<H5ReEntity>>() {
            @Override
            public void onSuccess(BaseEntity<H5ReEntity> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    //参数1:type(0:	前台,1:用户中心);	参数2:no
                    String url;
                    int companyType = SharedPreferenceUtil.getInstance().getSaveIntData(SharedPreferenceUtil.COM_TYPE, 0);
                    if (companyType == 2){
                        url = data.getData().getUrl() + "?no=" + no + "&type=1" + "&teamRoleNo="+SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.COMPANY_NO, "");
                    }else {
                        url = data.getData().getUrl() + "?no=" + no + "&type=1";
                    }
                    WebViewX5HostActivity.start(activity, url);
                }
            }
        });
    }

}
